package com.czl.module_service.fragment.inventory

import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.inventory.InventoryHomeAdapter
import com.czl.module_service.databinding.FragmentInventoryHomeAssetBinding
import com.czl.module_service.extension.loadCompletedData
import com.czl.module_service.viewmodel.inventory.InventoryHomeViewModel

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_INVENTORY_HOME)
class InventoryHomeFragment :
    BaseFragment<FragmentInventoryHomeAssetBinding, InventoryHomeViewModel>() {

    var result : Int = 0

    private val mAdapter: InventoryHomeAdapter by lazy {
        InventoryHomeAdapter(this)
    }

    override fun initContentView(): Int {
        return R.layout.fragment_inventory_home_asset
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()
        viewModel.tvTitle.set("资产盘点")
        binding.ryCommon.run {
            adapter = mAdapter
        }
        reload()
    }

    override fun onResume() {
        super.onResume()
        if (result != 0) {
            reload()
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        LiveBusCenter.observeInventoryHomeReturnEvent(this, {
            // 点击搜索查询
            result ++
        })
        viewModel.uc.loadCompleteEvent.observe(this) { data ->
            this.loadCompletedData(
                data?.hasNextPage,
                viewModel.pageNo == 2,
                data != null,
                data?.total,
                data?.list,
                mAdapter,
                binding.ryCommon,
                binding.smartCommon
            )
        }
    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
}